package com.dbManage.page.user;

import com.dbManage.dao.UserDao;
import com.dbManage.entity.Message;
import com.dbManage.entity.User;
import com.dbManage.exception.PasswordException;
import com.dbManage.exception.UserRoleException;
import com.dbManage.tools.SessionTool;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class UserController {
    @Resource
    UserService userService;
    @Resource
    UserDao userDao;

    /**
     * 用户列表
     * @param model
     * @return
     */
    @RequestMapping("/userList")
    public String uesrList(Model model){
        model.addAttribute("list",userService.getUserList());
        model.addAttribute("role", SessionTool.getUser().getRole());
        return "userList";
    }
    @RequestMapping("/queryUser")
    @ResponseBody
    public Message queryUser(User user){
        List<User> list=userService.queryUser(user);
        return new Message(Message.STATUS_SUCCESS,list,"success");
    }

    /**
     * 用户权限信息
     * @param model
     * @return
     */
    @RequestMapping("/userRole")
    public String userRole(Model model,int id) throws UserRoleException {
        model.addAttribute("user",userService.findUserById(id));
        return "userRole";
    }

    /**
     * 修改用户权限
     * @param user
     * @return
     */
    @RequestMapping("/updateUserRole")
    @ResponseBody
    public Message updateUserRole(User user){
        try {
            userService.updateUserRole(user);
            return new Message(Message.STATUS_SUCCESS,"修改成功");
        } catch (UserRoleException e) {
            return new Message(Message.STATUS_FAILED,e.getMessage());
        }
    }

    /**
     * 修改用户信息页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/userInfo")
    public String userInfo(Model model,int id) throws UserRoleException {
            model.addAttribute("user",userService.findUserById(id));
        return "userInfo";
    }
    /**
     *  修改用户信息
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public Message updateUser(User user){
        try {
            userService.updateUser(user);
            return new Message(Message.STATUS_SUCCESS,"修改成功");
        } catch (UserRoleException e) {
            return new Message(Message.STATUS_FAILED,e.getMessage());
        }
    }
    /**
     * 删除用户
     */
    @RequestMapping("/deleteUser")
    @ResponseBody
    public Message deleteUser(int id){
        try {
            userService.deleteUser(id);
            return new Message(Message.STATUS_SUCCESS,"删除成功");
        } catch (UserRoleException e) {
            return new Message(Message.STATUS_FAILED,e.getMessage());
        }

    }

    /**
     * 修改密码页
     */
    @RequestMapping("/chagePasswordView")
    public String chagePasswordView(){
        return "changePassword";
    }
    /**
     * 修改密码
     */
    @RequestMapping("/changePassword")
    @ResponseBody
    public Message changePassword(String password, String oldPassword){
        try {
            userService.updateUserPassword(password,oldPassword);
            return new Message(Message.STATUS_SUCCESS,"修改成功");
        } catch (PasswordException e) {
            return new Message(Message.STATUS_FAILED,e.getMessage());
        }
    }
}
